<%@ include file="/common/taglibs.jsp"%>
<%@ taglib prefix="probiz" tagdir="/WEB-INF/tags/probiz"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<c:if test="${not empty newsletter.newsletterId}">
	<!-- 针对新增时可以测试发送消息，发送后，原heading不变 -->
	<c:set var="entityName" value="${newsletter.newsletterId}"></c:set>
</c:if>
<probiz:pageHeading entityName="${entityName}" entityHeadingKey="newsletter.formTabTitle" />
<content tag="buttons">
<probiz:iconBtn btnType="save" onclick="return fnDoSave(this,'newsletterName');" />
<probiz:iconBtn btnType="saveAndNext" onclick="return fnDoSaveAndNext(this,'newsletterName');" />
<c:if test="${newsletter.newsletterId!=null}">
	<probiz:iconBtn btnType="delete" onclick="return fnDoDelete(this,'newsletterName');" />
</c:if>
<probiz:iconBtn btnType="back" onclick="return fnDoCancelForm(this);" />
<div class="line"></div>
<probiz:iconBtn btnType="sendTest" btnTextKey="newsletter.sendTest" onclick="fnShowSendTestDiv();" />
</content>

<div class="top clearfix">
	<ul class="tab">
		<li class="curr">
			<span><fmt:message key="newsletter.formTabTitle" />
			</span>
		</li>
	</ul>
</div>
<div class="modify">
	<form:form cssClass="mainForm" method="post" id="newsletter" commandName="newsletter"
		action="${ctxPath}/customer/newsletter.html" onsubmit="return fnOnSubmit(this);">
		<form:hidden path="version" />
		<input type="hidden" name="newsletterId" value="${newsletter.newsletterId}" />
		<div id="testDiv" style="display: none">
			<table width="100%" border="0" cellspacing="0" cellpadding="0" class="com_modi_table">
				<tr>
					<th colspan="2" class="title">
						<h4>
							<fmt:message key="newsletter.sendTest" />
						</h4>
					</th>
				</tr>
				<tr>
					<th class="label_width_1">
						<StoreAdmin:label key="newsletter.recipients" />
					</th>
					<td>
						<input type="text" id="recipients" name="recipients" value="" size="30" class="inputtxt"
							style="width: 350px" />
						&nbsp;&nbsp;&nbsp;
						<input type="button" class="" onclick="fnTestEmailNewsletter()"
							value="<fmt:message key="newsletter.sendTest"/>" />
					</td>
				</tr>
			</table>
		</div>

		<table width="100%" border="0" cellspacing="0" cellpadding="0" class="com_modi_table">
			<tr>
				<th colspan="2" class="title">
					<h4>
						<fmt:message key="newsletter.group.basic" />
					</h4>
				</th>
			</tr>
			<tr>
				<th class="label_width_1">
					<StoreAdmin:label key="newsletter.assignToGroup" colon="false" />
					(
					<span class="red">*</span>):
				</th>
				<td>
					<c:forEach items="${newsletterGroups}" var="item">
						<input id="${item.id }" name="newsletterGroupIds" validConf="required" type="checkbox"
							value="${item.id }" <c:if test="${item.checked}">checked="true"</c:if> />
						<label for="${item.id }">
							${item.name}
						</label>
							&nbsp;&nbsp;&nbsp;&nbsp;
					</c:forEach>
					<c:if test="${newsletterGroups==null || fn:length(newsletterGroups)==0}">
						<span class="empty_add"><fmt:message key="newsletter.helpMessage.tip"/>，<a
							href="${ctxPath}/customer/newsletterGroup.html?doAction=add"><fmt:message key="newsletter.helpMessage.click"/></a><fmt:message key="newsletter.helpMessage.add"/>。</span>
					</c:if>
				</td>
			</tr>
			<tr>
				<th>
					<StoreAdmin:label key="newsletter.title" />
				</th>
				<td>
					<spring:bind path="newsletter.title">
						<input class="inputtxt" type="text" name="${status.expression}" id="${status.expression}"
							value="${status.value}" style="width: 350px" maxlength="256" />
						<span class="fieldError">${status.errorMessage}</span>
					</spring:bind>
				</td>
			</tr>
			<tr>
				<th>
					<StoreAdmin:label key="newsletter.content" />
				</th>
				<td>
					<textarea id="content" name="content" cols="40" rows="15" class="textareatxt">${newsletter.content}</textarea>
					<probiz:plugin_tip id="fullDescription_tip"><fmt:message key="description.tip" /></probiz:plugin_tip>
					<%--textarea name="content" id="content" cols="60" rows="15" class="inputtxt">${newsletter.content}</textarea --%>
				</td>
			</tr>
			<tr>
				<th>
					<StoreAdmin:label key="newsletter.sendTime" />
				</th>
				<td>
					<spring:bind path="newsletter.sendTime">
						<input class="inputtxt" type="text" name="${status.expression}" id="${status.expression}"
							value="${status.value}" />
						<probiz:ui_datePicker outPut="${status.expression}" />
						<span class="fieldError">${status.errorMessage}</span>
					</spring:bind>
				</td>
			</tr>
			<tr>
				<th>
					<StoreAdmin:label key="newsletter.sendTo" />
				</th>
				<td>
					<input type="hidden" id="_toAllCustomers" name="_toAllCustomers" value="on" />
					<input type="checkbox" name="toAllCustomers" id="toAllCustomers" value="1"
						<c:if test="${newsletter.toAllCustomers eq 1}">checked="true"</c:if> />
					<label for="toAllCustomers">
						<fmt:message key="newsletter.toAllCustomers"></fmt:message>
					</label>
				</td>
			</tr>
			<c:if test="${not empty newsletter.newsletterId}">
				<tr>
					<th colspan="2" class="title">
						<h4>
							<fmt:message key="newsletter.group.others" />
						</h4>
					</th>
				</tr>
				<tr>
					<th>
						<StoreAdmin:label key="common.message.status" />
					</th>
					<td>
						<c:if test="${newsletter.status eq 0}">
							<fmt:message key="mailQueue.sendStatus0" />
						</c:if>
						<c:if test="${newsletter.status eq 1}">
							<fmt:message key="mailQueue.sendStatus2" />
						</c:if>
					</td>
				</tr>
				<c:set var="createTime">
					<fmt:formatDate value="${newsletter.createTime}" pattern="${appConfig.dateTimePattern}" />
				</c:set>
				<c:set var="updateTime">
					<fmt:formatDate value="${newsletter.updateTime}" pattern="${appConfig.dateTimePattern}" />
				</c:set>
				<probiz:formText label="common.message.createTime" value="${createTime}" />
				<probiz:formText label="common.message.updateTime" value="${updateTime}" />
			</c:if>
		</table>
	</form:form>
</div>

<probiz:plugin_htmlEditor textareaIds="content" />
<v:javascript formName="newsletter" staticJavascript="false" />
<script type="text/javascript">
	function fnOnSubmit(oForm){
		KE.util.setData('content');
		return validateNewsletter(oForm);
	}
	var errMsg = '<fmt:message key="newsletter.sendTest.required"/>';
    function fnTestEmailNewsletter(){
    	KE.util.setData('content');
    	if(!validateRequired( document.getElementById("recipients").value) || !validateRequired( document.getElementById("content").value)){
			alert(errMsg);
			return;
		}
		
		var errMsgEamil = '<fmt:message key="validate.email"/>'; 
		if(!validateEmail(document.getElementById("recipients").value)){
			alert(errMsgEamil);
			return;
		}
    	
   		var newsletterForm = document.getElementById("newsletter");
   		newsletterForm.action="${ctxPath}/customer/newsletter.html?doAction=testEmailNewletter";
   		newsletterForm.submit();
    }
      
    var isShow = false;
	function fnShowSendTestDiv(){
		if(!isShow){
			$j('#testDiv').css('display', 'block');
			isShow = true;
		}else{
			$j('#testDiv').css('display', 'none');
			isShow = false;
		}
	}
</script>
